package com.zy.modules.sys.service.impl;

import com.zy.modules.sys.entity.SysRoleMenuEntity;
import com.zy.modules.sys.repo.RoleMenuRepository;
import com.zy.modules.sys.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色与菜单对应关系
 *
 *
 *
 * @date 2016年9月18日 上午9:44:35
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Resource
    private RoleMenuRepository roleMenuRepository;

    @Override
    @Transactional
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        //先删除角色与菜单关系
        roleMenuRepository.delete(roleId);

        if (menuIdList.size() == 0) {
            return;
        }

        //保存角色与菜单关系
        List<SysRoleMenuEntity> entities = new ArrayList<>(menuIdList.size());
        for (Long menuId : menuIdList) {
            SysRoleMenuEntity entity = new SysRoleMenuEntity();
            entity.setRoleId(roleId);
            entity.setMenuId(menuId);
            entities.add(entity);
        }
        roleMenuRepository.save(entities);
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        return roleMenuRepository.findByRoleId(roleId);
    }

}
